package com.laizhenghua.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.laizhenghua.hotel.utils.PageUtils;
import com.laizhenghua.hotel.utils.Query;
import com.laizhenghua.hotel.vo.CatesVo;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.laizhenghua.hotel.dao.CatesDao;
import com.laizhenghua.hotel.entity.CatesEntity;
import com.laizhenghua.hotel.service.CatesService;


@Service("catesService")
public class CatesServiceImpl extends ServiceImpl<CatesDao, CatesEntity> implements CatesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CatesEntity> page = this.page(
                new Query<CatesEntity>().getPage(params),
                new QueryWrapper<CatesEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CatesVo> getByPid(int id) {
        List<CatesEntity> catesEntities = this.baseMapper.selectList(new QueryWrapper<CatesEntity>().eq("pid",id));

        List<CatesVo> catesVoList = catesEntities.stream().map(item -> {
            CatesVo catesVo = new CatesVo();
            catesVo.setId(item.getId());
            catesVo.setTitle(item.getTitle());
            return catesVo;
        }).collect(Collectors.toList());
        return  catesVoList;
    }

    @Override
    public boolean updateCates(Map<String, Object> params) {
        boolean isUpdate = this.update(new UpdateWrapper<CatesEntity>()
                .set("title", params.get("title"))
                .eq("id", params.get("id")));
        return isUpdate;
    }
}